package com.qqtop10.manager.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qqtop10.manager.model.Admin;
import com.qqtop10.manager.model.Brand;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.BrandService;
import com.qqtop10.manager.utils.LogHelper;

@RequestMapping("/manager/brand/")
@Controller
public class BrandController extends BaseController {
	@Autowired
	@Qualifier("brandService")
	private BrandService brandService;

	@RequestMapping("all.do")
	@ResponseBody
	public Result all(Brand input) {
		try {
			PagingArgs pagingArgs = getPagingArgs();
			return page(brandService.count(input), brandService.all(input, pagingArgs));
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("add.do")
	@ResponseBody
	public Result add() {
		try {
			Brand input = new Brand();
			fill(input);
			MultipartFile logo = getFile("photo");
			if (logo != null && !logo.isEmpty()) {
				input.setLogo(save(logo));
			}
			if (input == null || isNull(input.getLogo()) || isNull(input.getName())|| isNull(input.getUrl())) {
				return err("有些必须的参数没有指定。");
			}
			input.setUuid(newId());
			input.setCreatetime(now());
			input.setCreator(getLogindUuid());
			if (brandService.add(input)) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(),"后台品牌管理模块","添加成功",LogHelper.getLocalIp(getRequest()),Tempadmin.getAccount(),"",true);

				return html("操作成功！", input);
			}
			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("update.do")
	@ResponseBody
	public Result update() {
		Brand input = new Brand();
		fill(input);
		MultipartFile logo = getFile("photo");
		if (logo != null && !logo.isEmpty()) {
			input.setLogo(save(logo));
		}
		if (input == null || input.getId() == null || input.getLogo() == null || input.getName() == null) {
			return err("有些必须的参数未指定。");
		}
		try {
			input.setUpdatetime(now());
			input.setMender(getLogindUuid());
			if (brandService.update(input)) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(),"后台品牌管理模块","修改成功",LogHelper.getLocalIp(getRequest()),Tempadmin.getAccount(),"",true);

				return html("操作成功!", input);
			}
			return err("操作失败!");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@SuppressWarnings("deprecation")
	@RequestMapping("recommend.do")
	@ResponseBody
	public Result recommend(HttpServletRequest request, HttpServletResponse response) {
		
		Brand input=new Brand();
		DateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String id = request.getParameter("id");
		String recommend_start_time = request.getParameter("recommend_start_time");
		String recommend_end_time = request.getParameter("recommend_end_time");
		
		Date start_time = null;
		Date end_time = null;
		try {
			start_time = sf.parse(recommend_start_time);
			end_time = sf.parse(recommend_end_time);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		
		input.setId(Integer.parseInt(id));
		input.setRecommend_start_time(start_time);
		input.setRecommend_end_time(end_time);
		
		
		if (input == null || input.getId() == null || input.getRecommend_start_time() == null
				|| input.getRecommend_end_time() == null) {
			return err("有些必须的参数未指定。");
		}
		if(input.getRecommend_start_time().getTime() >= input.getRecommend_end_time().getTime()){
			return err("结束时间不能小于等于开始时间。");
		}
		try {
			input.setIsrecommend(1);
			input.setUpdatetime(now());
			input.setMender(getLogindUuid());
			if (brandService.recommend(input)) {
				return html("操作成功!", input);
			}
			return err("操作失败!");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("delete.do")
	@ResponseBody
	public Result delete(int[] ids) {
		try {
			Brand input = new Brand();
			input.setMender(getLogindUuid());
			input.setUpdatetime(now());
			int count = 0;
			for (int i = 0; i < ids.length; i++) {
				input.setId(ids[i]);
				if (!brandService.delete(input)) {
					ids[count] = ids[i];
					++count;
				}
			}
			if (count == 0) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(),"后台品牌管理模块","删除成功",LogHelper.getLocalIp(getRequest()),Tempadmin.getAccount(),"",true);

				return ok("操作成功！");
			} else if (count < ids.length) {
				return ok("操作已完成。但有些数据没有删除成功。");
			} else {
				return err("全部操作失败！");
			}
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("updateRecommend.do")
	@ResponseBody
	public Result updateRecommend(Brand input){
		int ret = brandService.updateRecommend(input);
		if(ret > 0){
			return ok("操作成功！");
		}
		return err("操作失败");
	}
}
